package top.maof.book.json;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.PropertyWriter;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.vdurmont.emoji.EmojiParser;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;

/**
 * 解码emoji
 * emojiStr --> emojiUnicode
 */
@Deprecated
public class EmojiFilter extends SimpleBeanPropertyFilter {

    @Override
    public void serializeAsField(Object pojo, JsonGenerator jgen,
                                 SerializerProvider provider, PropertyWriter writer)
            throws Exception {
        Field field = ReflectionUtils.findField(pojo.getClass(), writer.getFullName().getSimpleName());
        String emojiStr = field.get(pojo).toString();

        ReflectionUtils.setField(field, pojo, emojiStr);

        if (super.include(writer)) {
            writer.serializeAsField(pojo, jgen, provider);
        } else if (!jgen.canOmitFields()) {
            writer.serializeAsOmittedField(pojo, jgen, provider);
        }
    }
}
